<template>
  <div class="mod-user">
    <div class="listHead">
      <p v-if="status == 0">
        <el-button type="text" @click="saveForm()">保存</el-button>
        <!-- <el-button type="text" @click="dataFormSubmit"
          >生单<i class="el-icon-arrow-down"></i
        ></el-button> -->
        <!-- <el-button type="text" @click="goback">返回</el-button> -->
      </p>
      <p v-else>
        <el-button type="text" v-if="dataForm.status == 0">删除</el-button>
        <el-button
          type="text"
          v-if="dataForm.status == 0"
          @click="examineHandle(dataForm.id)"
          >审核</el-button
        >
        <el-button
          type="text"
          v-if="dataForm.status == 1"
          @click="unexamineHandle(dataForm.id)"
          >反审核</el-button
        >
        <!-- <el-button type="text" @click="goback">返回</el-button> -->
      </p>
    </div>
    <div class="headInfo">
      <el-form
        :inline="true"
        :model="dataForm"
        ref="dataForm"
        :rules="dataFormRule"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item
          ><em>单据编号</em>
          <div v-if="status == 0">
            <el-input
              v-model="dataForm.number"
              placeholder="单据编号"
              :disabled="true"
            ></el-input>
          </div>
          <div v-else class="see">
            <el-input
              v-model="dataForm.number"
              placeholder="单据编号"
              :disabled="true"
            ></el-input>
          </div>
        </el-form-item>
        <el-form-item>
          <em>单据日期</em>
          <div class="block" v-if="status == 0">
            <el-date-picker
              v-model="dataForm.dateTime"
              type="datetime"
              placeholder="选择日期时间"
              :disabled="true"
            >
            </el-date-picker>
          </div>
          <div v-else class="see">
            <el-date-picker
              v-model="dataForm.dateTime"
              type="datetime"
              placeholder="选择日期时间"
              :disabled="true"
            >
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item
          ><em>其他出库单号</em>
          <div v-if="status == 0">
            <el-input
              v-model="dataForm.otherdelivery"
              placeholder="其他出库单号"
              maxlength="20"
              :disabled="true"
            ></el-input>
          </div>
          <div v-else class="see">
            <el-input
              v-model="dataForm.otherdelivery"
              placeholder="其他出库单号"
              maxlength="20"
              :disabled="true"
            ></el-input>
          </div>
        </el-form-item>
        <el-form-item
          ><em>其他入库单号</em>
          <div v-if="status == 0">
            <el-input
              v-model="dataForm.otherstock"
              placeholder="其他入库单号"
              maxlength="30"
              :disabled="true"
            ></el-input>
          </div>
          <div v-else class="see">
            <el-input
              v-model="dataForm.otherstock"
              placeholder="其他入库单号"
              maxlength="30"
              :disabled="true"
            ></el-input>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="main" v-if="status == 0">
      <div>
        <h2>转出明细</h2>
        <el-form
          ref="dataListin"
          size="small"
          class="form1"
          :model="dataForm"
          :rules="dataListinRule"
        >
          <template>
            <el-table
              :data="dataForm.dataListin"
              border
              v-loading="dataListLoading"
              @selection-change="selectionChangeHandle"
              style="width: 100%"
              :summary-method="getSummaries"
              show-summary
            >
              <el-table-column
                label="序号"
                type="index"
                align="center"
                show-overflow-tooltip
                width="50px"
              >
              </el-table-column>
              <el-table-column
                prop="mBarCode"
                header-align="center"
                align="center"
                label="货品编号"
                class="mBarCode"
              >
                <template slot-scope="scope">
                  <el-form-item
                    :prop="'dataListin.' + scope.$index + '.mBarCode'"
                    :rules="dataListinRule.mBarCode"
                  >
                    <el-input
                      style="width: 100%"
                      placeholder="请选择"
                      v-model="scope.row.mBarCode"
                      :suffix-icon="
                        showTree ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
                      "
                      @click.native="deptogglePanel(scope.$index, 'in')"
                      size="medium"
                      @input="input($event)"
                      readonly
                    ></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                prop="name"
                header-align="center"
                align="center"
                label="货品名称"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.name"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="standard"
                header-align="center"
                align="center"
                label="规格型号"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.standard"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="DepotName"
                header-align="center"
                align="center"
                label="仓库"
              >
                <template slot-scope="scope">
                  <el-form-item
                    :prop="'dataListin.' + scope.$index + '.DepotName'"
                    :rules="dataListinRule.DepotName"
                  >
                    <el-select
                      v-model="scope.row.DepotName"
                      clearable
                      @change="change($event, scope.$index)"
                    >
                      <el-option
                        v-for="item in DepotOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                prop="number"
                header-align="center"
                align="center"
                label="数量"
              >
                <template slot-scope="scope">
                  <el-input v-model.number="scope.row.number"></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="unit"
                header-align="center"
                align="center"
                label="计量单位"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.unit"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="tax"
                header-align="center"
                align="center"
                label="税率"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.tax" :disabled="true"></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="lowDecimal"
                header-align="center"
                align="center"
                label="含税单价"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.lowDecimal"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="AllPrice" align="center" label="含税金额">
                <template scope="scope">
                  <el-input
                    size="mini"
                    :value="Pricefloatin(scope.$index)"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="stock"
                header-align="center"
                align="center"
                label="现存量"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.stock"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="stock"
                header-align="center"
                align="center"
                label="可用量"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.stock"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="remark"
                header-align="center"
                align="center"
                label="备注"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.remark"></el-input>
                </template>
              </el-table-column>
              <el-table-column
                fixed="right"
                header-align="center"
                align="center"
                label="操作"
                width="170px"
                v-if="status == 0"
              >
                <template slot-scope="scope">
                  <el-button
                    v-if="isAuth('sys:user:update')"
                    type="text"
                    size="small"
                    @click="addRowin"
                    >增行</el-button
                  >
                  <el-button
                    v-if="isAuth('sys:user:delete')"
                    type="text"
                    size="small"
                    @click="deleteRow(scope.$index, dataForm.dataListin)"
                    style="color: #fa7f05"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-form>
      </div>
      <div>
        <h2>转入明细</h2>
        <el-form
          ref="dataList"
          size="small"
          class="form1"
          :model="dataForm"
          :rules="dataListRule"
        >
          <template>
            <el-table
              :data="dataForm.dataListout"
              border
              v-loading="dataListLoading"
              @selection-change="selectionChangeHandle"
              style="width: 100%"
              :summary-method="getSummaries"
              show-summary
            >
              <el-table-column
                label="序号"
                type="index"
                align="center"
                show-overflow-tooltip
                width="50px"
              >
              </el-table-column>
              <el-table-column
                prop="mBarCode"
                header-align="center"
                align="center"
                label="货品编号"
                class="mBarCode"
              >
                <template slot-scope="scope">
                  <el-form-item
                    :prop="'dataListout.' + scope.$index + '.mBarCode'"
                    :rules="dataListRule.mBarCode"
                  >
                    <el-input
                      style="width: 100%"
                      placeholder="请选择"
                      v-model="scope.row.mBarCode"
                      :suffix-icon="
                        showTree ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
                      "
                      @click.native="deptogglePanel(scope.$index, 'out')"
                      size="medium"
                      @input="input($event)"
                      readonly
                    ></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                prop="name"
                header-align="center"
                align="center"
                label="货品名称"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.name"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="standard"
                header-align="center"
                align="center"
                label="规格型号"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.standard"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="DepotName"
                header-align="center"
                align="center"
                label="仓库"
              >
                <template slot-scope="scope">
                  <el-form-item
                    :prop="'dataListout.' + scope.$index + '.DepotName'"
                    :rules="dataListRule.DepotName"
                  >
                    <el-select
                      v-model="scope.row.DepotName"
                      clearable
                      @change="change($event, scope.$index)"
                    >
                      <el-option
                        v-for="item in DepotOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                prop="number"
                header-align="center"
                align="center"
                label="数量"
              >
                <template slot-scope="scope">
                  <el-input v-model.number="scope.row.number"></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="unit"
                header-align="center"
                align="center"
                label="计量单位"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.unit"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="tax"
                header-align="center"
                align="center"
                label="税率"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.tax" :disabled="true"></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="lowDecimal"
                header-align="center"
                align="center"
                label="含税单价"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.lowDecimal"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="AllPrice" label="含税金额">
                <template scope="scope">
                  <el-input
                    size="mini"
                    :value="Pricefloatout(scope.$index)"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="stock"
                header-align="center"
                align="center"
                label="现存量"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.stock"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="stock"
                header-align="center"
                align="center"
                label="可用量"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.stock"
                    :disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="remark"
                header-align="center"
                align="center"
                label="备注"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.remark"></el-input>
                </template>
              </el-table-column>
              <el-table-column
                fixed="right"
                header-align="center"
                align="center"
                label="操作"
                width="170px"
                v-if="status == 0"
              >
                <template slot-scope="scope">
                  <el-button
                    v-if="isAuth('sys:user:update')"
                    type="text"
                    size="small"
                    @click="addRowout"
                    >增行</el-button
                  >
                  <el-button
                    v-if="isAuth('sys:user:delete')"
                    type="text"
                    size="small"
                    @click="deleteRow(scope.$index, dataForm.dataListout)"
                    style="color: #fa7f05"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-form>
      </div>
    </div>
    <div class="main"  v-else>
      <div>
        <h2>转出明细</h2>
        <el-form
          ref="dataList"
          size="small"
          class="form1"
          :model="dataForm"
          :rules="dataListinRule"
        >
          <template>
            <el-table
              :data="dataForm.dataListin"
              border
              v-loading="dataListLoading"
              @selection-change="selectionChangeHandle"
              style="width: 100%"
              :summary-method="getSummaries"
              show-summary
            >
              <el-table-column
                label="序号"
                type="index"
                align="center"
                show-overflow-tooltip
                width="50px"
              >
              </el-table-column>
              <el-table-column
                prop="mBarCode"
                header-align="center"
                align="center"
                label="货品编号"
                class="mBarCode"
              >
              </el-table-column>
              <el-table-column
                prop="name"
                header-align="center"
                align="center"
                label="货品名称"
              >
              </el-table-column>
              <el-table-column
                prop="standard"
                header-align="center"
                align="center"
                label="规格型号"
              >
              </el-table-column>
              <el-table-column
                prop="DepotName"
                header-align="center"
                align="center"
                label="仓库"
              >
              </el-table-column>
              <el-table-column
                prop="number"
                header-align="center"
                align="center"
                label="数量"
              >
              </el-table-column>
              <el-table-column
                prop="unit"
                header-align="center"
                align="center"
                label="计量单位"
              >
              </el-table-column>
              <el-table-column
                prop="tax"
                header-align="center"
                align="center"
                label="税率"
              >
              </el-table-column>
              <el-table-column
                prop="lowDecimal"
                header-align="center"
                align="center"
                label="含税单价"
              >
              </el-table-column>
              <el-table-column prop="AllPrice" align="center" label="含税金额">
              </el-table-column>
              <el-table-column
                prop="stock"
                header-align="center"
                align="center"
                label="现存量"
              >
              </el-table-column>
              <el-table-column
                prop="stock"
                header-align="center"
                align="center"
                label="可用量"
              >
              </el-table-column>
              <el-table-column
                prop="remark"
                header-align="center"
                align="center"
                label="备注"
              >
              </el-table-column>
            </el-table>
          </template>
        </el-form>
      </div>
      <div>
        <h2>转入明细</h2>
        <el-form
          ref="dataList"
          size="small"
          class="form1"
          :model="dataForm"
          :rules="dataListRule"
        >
          <template>
            <el-table
              :data="dataForm.dataListout"
              border
              v-loading="dataListLoading"
              @selection-change="selectionChangeHandle"
              style="width: 100%"
              :summary-method="getSummaries"
              show-summary
            >
              <el-table-column
                label="序号"
                type="index"
                align="center"
                show-overflow-tooltip
                width="50px"
              >
              </el-table-column>
              <el-table-column
                prop="mBarCode"
                header-align="center"
                align="center"
                label="货品编号"
                class="mBarCode"
              >
              </el-table-column>
              <el-table-column
                prop="name"
                header-align="center"
                align="center"
                label="货品名称"
              >
              </el-table-column>
              <el-table-column
                prop="standard"
                header-align="center"
                align="center"
                label="规格型号"
              >
              </el-table-column>
              <el-table-column
                prop="DepotName"
                header-align="center"
                align="center"
                label="仓库"
              >
              </el-table-column>
              <el-table-column
                prop="number"
                header-align="center"
                align="center"
                label="数量"
              >
              </el-table-column>
              <el-table-column
                prop="unit"
                header-align="center"
                align="center"
                label="计量单位"
              >
              </el-table-column>
              <el-table-column
                prop="tax"
                header-align="center"
                align="center"
                label="税率"
              >
              </el-table-column>
              <el-table-column
                prop="lowDecimal"
                header-align="center"
                align="center"
                label="含税单价"
              >
              </el-table-column>
              <el-table-column prop="AllPrice" label="含税金额">
              </el-table-column>
              <el-table-column
                prop="stock"
                header-align="center"
                align="center"
                label="现存量"
              >
              </el-table-column>
              <el-table-column
                prop="stock"
                header-align="center"
                align="center"
                label="可用量"
              >
              </el-table-column>
              <el-table-column
                prop="remark"
                header-align="center"
                align="center"
                label="备注"
              >
              </el-table-column>
            </el-table>
          </template>
        </el-form>
      </div>
    </div>
    <div class="footer" v-if="status == 0">
      <el-input
        type="textarea"
        :rows="5"
        placeholder="请输入备注"
        v-model="dataForm.remark"
        resize="none"
      >
      </el-input>
    </div>
    <div class="footer" v-else>
      {{ dataForm.remark }}
    </div>
    <goodsListDialog
      v-if="isfind"
      :isfind="isfind"
      @changeFind="getFind"
      @getGoods="getGoodsinfo"
    ></goodsListDialog>
    <customerDialog
      v-if="isCustomer"
      :isCustomer="isCustomer"
      @changeCustomer="getCustomer"
      @getCustomer="getCustomerinfo"
    ></customerDialog>
  </div>
</template>
<script>
import goodsListDialog from "@/views/common/goodsListDialog";
import customerDialog from "@/views/common/customerDialog";
export default {
  components: {
    goodsListDialog,
    customerDialog,
  },
  data() {
    return {
      dataForm: {
        number: "",
        dateTime: "",
        customer: "",
        otherdelivery: "",
        otherstock: "",
        remark: "",
        dataListin: [],
        dataListout: [],
      },
      otherstockBillNo: "",
      inoroutstatus: "",
      status: 0,
      options4: [
        {
          value: "选项1",
          label: "平台配送",
        },
        {
          value: "选项2",
          label: "第三方物流",
        },
        {
          value: "选项3",
          label: "上门自提",
        },
      ],
      dataList: [],
      options2: [],
      options3: [
        {
          value: "选项1",
          label: "采购入库",
        },
        {
          value: "选项2",
          label: "自制产品入库",
        },
        {
          value: "选项3",
          label: "借入",
        },
        {
          value: "选项4",
          label: "借出还入",
        },
      ],
      DepotOptions: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      total: null,
      dataListLoading: false,
      isfind: false,
      isCustomer: false,
      iscustomer: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      value1: [],
      beginTime: "",
      endTime: "",
      index: "",
      dataFormRule: {
        stockType: [{ required: true, message: " ", trigger: "change" }],
      },
      dataListRule: {
        mBarCode: [{ required: true, message: " ", trigger: "change" }],
        DepotName: [{ required: true, message: " ", trigger: "change" }],
      },
      dataListinRule: {
        mBarCode: [{ required: true, message: " ", trigger: "change" }],
        DepotName: [{ required: true, message: " ", trigger: "change" }],
      },
      showTree: false,
      organId: "",
    };
  },
  activated() {
    this.getData();
    this.getGoodsinfo();
    this.getCustomerinfo();
    this.getDepotOptions();
  },
  methods: {
    // 反审核
    unexamineHandle(id) {
      this.$http({
        url: this.$http.adornUrl(`/depotHead/batchSetStatus`),
        method: "post",
        data: this.$http.adornData(
          {
            depotHeadIDs: id.toString(),
            status: "0",
          },
          false
        ),
      }).then(({ data }) => {
        // console.log("data is :", data);
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.getData();
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 审核
    examineHandle(id) {
      console.log(typeof id);
      this.$http({
        url: this.$http.adornUrl(`/depotHead/batchSetStatus`),
        method: "post",
        data: this.$http.adornData(
          {
            depotHeadIDs: id.toString(),
            status: "1",
          },
          false
        ),
      }).then(({ data }) => {
        // console.log("data is :", data);
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 2000,
            onClose: () => {
              this.getData();
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 获取仓库信息
    getDepotOptions() {
      this.$http({
        url: this.$http.adornUrl("/depot/findDepotByUserId"),
        method: "get",
        params: this.$http.adornParams({
          UBType: "UserDepot",
          UBKeyId: 63,
        }),
      }).then(({ data }) => {
        console.log(data);
        if (data && data.code === 0) {
          this.DepotOptions = data.data.filter((fitem) => {
            fitem.value = fitem.id;
            fitem.label = fitem.depotName;
            return fitem;
          });
        }
      });
    },
    // 尾行合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (index === 1) {
          sums[index] = "";
          return;
        }
        if (index === 2) {
          sums[index] = "";
          return;
        }
        if (index === 3) {
          sums[index] = "";
          return;
        }
        if (index === 4) {
          sums[index] = "";
          return;
        }
        if (index === 6) {
          sums[index] = "";
          return;
        }
        if (index === 7) {
          sums[index] = "";
          return;
        }
        if (index === 8) {
          sums[index] = "";
          return;
        }
        if (index === 10) {
          sums[index] = "";
          return;
        }
        if (index === 11) {
          sums[index] = "";
          return;
        }
        if (index === 12) {
          sums[index] = "";
          return;
        }
        if (index === 13) {
          sums[index] = "";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += "";
        } else {
          sums[index] = "N/A";
        }
      });
      return sums;
    },
    //总价的依赖属性
    Pricefloatin(index) {
      this.dataForm.dataListin[index].AllPrice =
        this.dataForm.dataListin[index].lowDecimal *
        this.dataForm.dataListin[index].number;
      return (
        this.dataForm.dataListin[index].lowDecimal *
          this.dataForm.dataListin[index].number || null
      );
    },
    //总价的依赖属性
    Pricefloatout(index) {
      this.dataForm.dataListout[index].AllPrice =
        this.dataForm.dataListout[index].lowDecimal *
        this.dataForm.dataListout[index].number;
      return (
        this.dataForm.dataListout[index].lowDecimal *
          this.dataForm.dataListout[index].number || null
      );
    },
    // 选择仓库
    change(e, index) {
      console.log(e, index);
      if (this.dataForm.dataList[index].mBarCode == "") {
        return;
      }
      // this.getstock(e.id, this.dataForm.dataList[index].MaterialExtendId);
    },
    // 获取库存信息
    // getstock(depotId, mId) {
    //   this.$http({
    //     url: this.$http.adornUrl(
    //       `/depotItem/findStockNumById?depotId=${depotId}&mId=${mId}`
    //     ),
    //     method: "get",
    //   }).then(({ data }) => {
    //     console.log(data);
    //     this.dataForm.dataList[this.curSelectIndex].Stock = data.data.stock;
    //   });
    // },
    // 获取仓库信息
    // getDepotOptions() {
    //   this.$http({
    //     url: this.$http.adornUrl("/depot/findDepotByUserId"),
    //     method: "get",
    //     params: this.$http.adornParams({
    //       UBType: "UserDepot",
    //       UBKeyId: 63,
    //     }),
    //   }).then(({ data }) => {
    //     if (data && data.code === 0) {
    //       this.DepotOptions = data.data.filter((fitem) => {
    //         fitem.value = fitem.id;
    //         fitem.label = fitem.depotName;
    //         return fitem;
    //       });
    //     }
    //   });
    // },
    // 获取传过来的商品信息
    getGoodsinfo(val) {
      console.log(val);
      if (!val) {
        return;
      }
      console.log(this.inoroutstatus);
      if (this.inoroutstatus == "in") {
        this.dataForm.dataListin[this.index].name = val.name;
        this.dataForm.dataListin[this.index].meId = val.meId;
        this.dataForm.dataListin[this.index].mBarCode = val.mBarCode;
        this.dataForm.dataListin[this.index].standard = val.standard;
        this.dataForm.dataListin[this.index].unit = val.unit;
        this.dataForm.dataListin[this.index].remark = val.remark;
        this.dataForm.dataListin[this.index].lowDecimal = val.lowDecimal;
        this.dataForm.dataListin[this.index].stock = val.stock;
        this.dataForm.dataListin[this.index].number = 1;
      } else {
        this.dataForm.dataListout[this.index].name = val.name;
        this.dataForm.dataListout[this.index].meId = val.meId;
        this.dataForm.dataListout[this.index].mBarCode = val.mBarCode;
        this.dataForm.dataListout[this.index].standard = val.standard;
        this.dataForm.dataListout[this.index].unit = val.unit;
        this.dataForm.dataListout[this.index].remark = val.remark;
        this.dataForm.dataListout[this.index].lowDecimal = val.lowDecimal;
        this.dataForm.dataListout[this.index].stock = val.stock;
        this.dataForm.dataListout[this.index].number = 1;
      }
    },
    // 获取传过来的客户信息
    getCustomerinfo(val) {
      if (!val) {
        return;
      }
      console.log(val.id);
      this.dataForm.customer = val.supplier;
      this.organId = val.id;

      console.log(this.dataForm.customer);
    },
    // 获取传过来的商品信息
    getFind(val) {
      console.log(val);
      this.isfind = val;
    },
    deptogglePanel(index, status) {
      console.log(status);
      this.inoroutstatus = status;
      this.index = index;
      this.isfind = true;
    },
    // 获取传过来的客户信息
    getCustomer(val) {
      console.log(val);
      this.isCustomer = false;
    },
    goCustomer() {
      this.isCustomer = true;
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 表格默认有一行 增行
    addRowin() {
      this.dataForm.dataListin.push({
        mBarCode: "",
        name: "",
        standard: "",
        DepotName: "",
        number: "",
        unit: "",
        tax: "",
        lowDecimal: "",
        stock: "",
        remark: "",
        AllPrice: "",
      });
    },
    // 表格默认有一行 增行
    addRowout() {
      this.dataForm.dataListout.push({
        mBarCode: "",
        name: "",
        standard: "",
        DepotName: "",
        number: "",
        unit: "",
        tax: "",
        lowDecimal: "",
        stock: "",
        remark: "",
        AllPrice: "",
      });
    },
    // 插行
    insertRow(index, rows) {
      rows.splice(index + 1, 0, {
        mBarCode: "",
        name: "",
        standard: "",
        DepotName: "",
        number: "",
        unit: "",
        tax: "",
        lowDecimal: "",
        stock: "",
        remark: "",
      });
    },
    // 复制
    copyRow(index, rows, row) {
      // console.log(index, rows, row)
      rows.splice(index + 1, 0, row);
    },
    // 删除
    deleteRow(index, rows) {
      //删除改行
      rows.splice(index, 1);
    },
    // 获取页面数据
    getData() {
      console.log(this.$route.query.update);
      if (this.$route.query.id) {
        let type = "转换";
        let subType = "形态转换";
        let roleType = "全部数据";
        let str = `{"type":"${type}","subType":"${subType}","roleType":"${roleType}","state":"","number":"","beginTime":"","endTime":"","materialParam":"","userName":"","payType":"","serviceType":"","transportType":"","status":"","depotIds":"","deliverDate":"","salesDate":"","billType":"","externalNumber":"","stockType":"","deliveryType":"",deliveryBillNo:"",salesBillNo:"","buyer":"","organName":"","purchaseStockBillNo":"","purchaseSalesBillNo":""}`;
        var search = encodeURIComponent(str);
        this.$http({
          url: this.$http.adornUrl(
            `/depotHead/getModalShiftList?search=${search}`
          ),
          method: "get",
          params: this.$http.adornParams({
            currentPage: 1,
            pageSize: 100000,
          }),
        }).then(({ data }) => {
          data.data.list.map((item) => {
            if (item.id == this.$route.query.id) {
              console.log(item);
              this.dataForm.number = item.number;
              this.dataForm.remark = item.remark;
              this.dataForm.trackingNumber = item.trackingNumber;
              this.dataForm.otherstockBillNo = item.otherstockBillNo;
              this.dataForm.stockType = item.stockType;
              this.dataForm.transportType = item.transportType;
              this.dataForm.dateTime = item.operTime;
              this.dataForm.customer = item.organName;
              this.dataForm.id = item.id;
              this.dataForm.status = item.status;
              this.dataForm.otherdelivery = item.otherdelivery;
              this.dataForm.otherstock = item.otherstock;
            }
          });
        });
        this.$http({
          url: this.$http.adornUrl(`/depotItem/getModalShiftList`),
          method: "get",
          params: this.$http.adornParams(
            {
              headerId: this.$route.query.id,
              mpList: "undefined%2Cundefined%2Cundefined",
            },
            false
          ),
        }).then(({ data }) => {
          console.log(data.data.rows);
          let dataList = JSON.parse(JSON.stringify(data.data.rows));
          let dataListin = [];
          let dataListout = [];
          dataList.map((item) => {
            console.log(item);
            item.mBarCode = item.MaterialName.split("_")[0];
            item.name = item.MaterialName.split("_")[1].split("(")[0];
            item.standard = item.MaterialName.split("_")[1]
              .split("(")[1]
              .split(")")[0];
            item.number = item.OperNumber;
            item.unit = item.Unit;
            item.tax = item.TaxRate;
            item.lowDecimal = item.UnitPrice;
            item.stock = item.Stock;
            item.remark = item.Remark;
            if (item.otherStatus == 1) {
              dataListin.push(item);
            } else {
              dataListout.push(item);
            }
          });
          console.log(dataListin, dataListout);
          this.dataForm.dataListin = dataListin;
          this.dataForm.dataListout = dataListout;
        });
        if (this.$route.query.update) {
          this.status = 0;
        } else {
          console.log(222);
          this.status = 1;
        }
      } else {
        this.status = 0;
        if (this.$route.query.gosalein) {
          console.log(11111);
        } else {
          this.dataForm.otherstockBillNo = "";
          this.dataForm.trackingNumber = "";
          this.dataForm.remark = "";
          this.dataForm.dataListin = [];
          this.dataForm.dataListout = [];
          console.log(this.dataForm.dataListin.length);
          if (
            this.dataForm.dataListin.length > 0 &&
            this.dataForm.dataListout.length > 0
          ) {
          } else {
            this.addRowin();
            this.addRowout();
          }
        }
        let num1 = "";
        let num2 = "";
        let num3 = "";
        this.$http({
          url: this.$http.adornUrl("/depotHead/buildNumber"),
          method: "get",
        }).then(({ data }) => {
          num1 = "XTZH" + data.DefaultNumber;
          num2 = "QTCK" + data.DefaultNumber;
          num3 = "QTRK" + data.DefaultNumber;
          this.$set(this.dataForm, "number", num1);
          this.$set(this.dataForm, "otherdelivery", num2);
          this.$set(this.dataForm, "otherstock", num3);
        });
        this.$set(this.dataForm, "dateTime", new Date());
      }
      if (sessionStorage.getItem("dataForm")) {
        this.dataForm = JSON.parse(sessionStorage.getItem("dataForm"));
        let otherstockBillNo = JSON.parse(sessionStorage.getItem("dataForm"))
          .number;
        this.dataForm.otherstockBillNo = otherstockBillNo;
        this.otherstockBillNo = otherstockBillNo;
        let num = "";
        this.$http({
          url: this.$http.adornUrl("/depotHead/buildNumber"),
          method: "get",
        }).then(({ data }) => {
          num1 = "XTZH" + data.DefaultNumber;
          num2 = "QTCK" + data.DefaultNumber;
          num3 = "QTRK" + data.DefaultNumber;
          this.$set(this.dataForm, "number", num1);
          this.$set(this.dataForm, "otherdelivery", num2);
          this.$set(this.dataForm, "otherstock", num3);
          this.number = num1;
        });
        this.$set(this.dataForm, "dateTime", new Date());
      }
    },
    // 生单
    dataFormSubmit() {},
    // 保存
    saveForm() {
      let dataFormvalid = null;
      let dataListvalid = null;
      this.$refs["dataListin"].validate((valid1) => {
        dataFormvalid = valid1;
      });
      this.$refs["dataList"].validate((valid2) => {
        dataListvalid = valid2;
      });
      if (dataFormvalid && dataListvalid) {
        if (this.$route.query.id) {
          let total = null;
          this.dataForm.dataListin.map((item) => {
            total += item.number;
          });
          this.dataForm.dataListout.map((item) => {
            total += item.number;
          });
          this.total = total;
          console.log(this.total);
          let info = `{"Type":"转换","SubType":"形态转换","DefaultNumber":"${this.dataForm.number}","Number":"${this.dataForm.number}","total":"${this.total}","LinkNumber":"","OperTime":"${this.dataForm.dateTime}","OrganId":"","Salesman":"","AccountId":"","ChangeAmount":"",TotalPrice":"","PayType":"","otherstockBillNo":"","trackingNumber":"","Remark":"${this.dataForm.remark}","stockType":"","transportType":"","AccountMoneyList":"","Discount":"","DiscountMoney":"","DiscountLastMoney":"","OtherMoney":"","otherdelivery":"${this.dataForm.otherdelivery}","otherstock":"${this.dataForm.otherstock}"}`;
          let rows = `[
              ${this.dataForm.dataListin
                .map((iitem) => {
                  console.log(iitem);
                  return `{"DepotId":"${
                    iitem.DepotName.id || iitem.DepotId
                  }","DepotName":"${
                    iitem.DepotName.depotName || iitem.DepotName
                  }","MaterialExtendId":"${
                    iitem.meId || iitem.MaterialExtendId
                  }","Stock":"${iitem.Stock || ""}","Unit":"${
                    iitem.unit
                  }","OperNumber":"${iitem.number}","UnitPrice":"${
                    iitem.lowDecimal
                  }","AllPrice":"${this.AllPrice || ""}","Remark":"${
                    iitem.remark
                  }","otherStatus":"1"},`;
                })
                .join("")}${this.dataForm.dataListout
            .map((iitem) => {
              console.log(iitem);
              return `{"DepotId":"${
                iitem.DepotName.id || iitem.DepotId
              }","DepotName":"${
                iitem.DepotName.depotName || iitem.DepotName
              }","MaterialExtendId":"${
                iitem.meId || iitem.MaterialExtendId
              }","Stock":"${iitem.Stock || ""}","Unit":"${
                iitem.unit
              }","OperNumber":"${iitem.number}","UnitPrice":"${
                iitem.lowDecimal
              }","AllPrice":"${this.AllPrice || ""}","Remark":"${
                iitem.remark
              }","otherStatus":"2"},`;
            })
            .join("")}
            ]`;
          this.$http({
            url: this.$http.adornUrl(
              `/depotHead/updateModalShiftAndDetail?id=${this.$route.query.id}`
            ),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.$route.query.id,
                info,
                preTotalPrice: this.AllPrice,
                rows,
              },
              false
            ),
          }).then((data) => {
            console.log(data);
            // this.visible = false;
            if (data.data.code === 0) {
              this.btnstatus = true;
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
              });
              this.$router.go(-1);
              this.removeTabHandle(this.mainTabsActiveName);
            } else {
              this.$message.error(data.msg);
            }
          });
        } else {
          let total = null;
          this.dataForm.dataListin.map((item) => {
            total += item.number;
          });
          this.dataForm.dataListout.map((item) => {
            total += item.number;
          });
          this.total = total;
          console.log(this.dataForm.dateTime);
          let OperTime = this.timestampToTime(this.dataForm.dateTime.getTime());
          let info = `{"Type":"转换","SubType":"形态转换","DefaultNumber":"${this.dataForm.number}","otherstockBillNo":"","Number":"${this.dataForm.number}","LinkNumber":"","total":"","OperTime":"${OperTime}","OrganId":"","trackingNumber":"","Salesman":"","AccountId":"","ChangeAmount":"","TotalPrice":"","PayType":"","Remark":"${this.dataForm.remark}",stockType:"",transportType:"","AccountMoneyList":"","Discount":"","DiscountMoney":"","DiscountLastMoney":"","OtherMoney":"","otherdelivery":"${this.dataForm.otherdelivery}","otherstock":"${this.dataForm.otherstock}"}`;
          let rows = `[
              ${this.dataForm.dataListin
                .map((iitem) => {
                  console.log(iitem);
                  return `{"DepotId":"${
                    iitem.DepotName.id || iitem.DepotId
                  }","DepotName":"${
                    iitem.DepotName.depotName || iitem.DepotName
                  }","MaterialExtendId":"${
                    iitem.meId || iitem.MaterialExtendId
                  }","Stock":"${iitem.Stock || ""}","Unit":"${
                    iitem.unit
                  }","OperNumber":"${iitem.number}","UnitPrice":"${
                    iitem.lowDecimal
                  }","AllPrice":"${this.AllPrice || ""}","Remark":"${
                    iitem.remark
                  }","otherStatus":"1"},`;
                })
                .join("")}${this.dataForm.dataListout
            .map((iitem) => {
              console.log(iitem);
              return `{"DepotId":"${
                iitem.DepotName.id || iitem.DepotId
              }","DepotName":"${
                iitem.DepotName.depotName || iitem.DepotName
              }","MaterialExtendId":"${
                iitem.meId || iitem.MaterialExtendId
              }","Stock":"${iitem.Stock || ""}","Unit":"${
                iitem.unit
              }","OperNumber":"${iitem.number}","UnitPrice":"${
                iitem.lowDecimal
              }","AllPrice":"${this.AllPrice || ""}","Remark":"${
                iitem.remark
              }","otherStatus":"2"},`;
            })
            .join("")}
            ]`;
          this.$http({
            url: this.$http.adornUrl(`/depotHead/addModalShiftAndDetail`),
            method: "post",
            data: this.$http.adornData(
              {
                info,
                rows,
              },
              false
            ),
          }).then((data) => {
            if (data.data.code === 0) {
              this.btnstatus = true;
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
              });
              this.$router.go(-1);
              this.removeTabHandle(this.mainTabsActiveName);
            } else {
              console.log(data.data.msg);
              this.$message({
                message: `${data.data.msg}`,
                type: "error",
                duration: 1500,
              });
            }
          });
        }
      } else {
        this.$message({
          message: "请补全信息后重试",
          type: "warning",
          duration: 1500,
        });
      }
    },
    // 时间戳转成时间
    timestampToTime(timestamp) {
      var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + "-";
      var M =
        (date.getHours() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D =
        (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
      var h =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var m =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var s =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return Y + M + D + h + m + s;
    },
    // 返回上一目录
    goback() {
      this.$router.go(-1);
      sessionStorage.removeItem("dataForm");
      this.removeTabHandle(this.mainTabsActiveName);
    },
    // tabs, 删除tab
    removeTabHandle(tabName) {
      console.log(tabName);
      console.log(tabName);
      this.mainTabs = this.mainTabs.filter((item) => item.name !== tabName);
      if (this.mainTabs.length >= 1) {
        // 当前选中tab被删除
        if (tabName === this.mainTabsActiveName) {
          var tab = this.mainTabs[this.mainTabs.length - 1];
          this.$router.push(
            { name: tab.name, query: tab.query, params: tab.params },
            () => {
              this.mainTabsActiveName = this.$route.name;
            }
          );
        }
      } else {
        this.menuActiveName = "";
        this.$router.push({ name: "home" });
      }
    },
  },
  computed: {
    // AllPrice: {
    //   get: function () {
    //     let total = 0;
    //     if (this.dataForm.dataList.length == 0) {
    //       return;
    //     }
    //     this.dataForm.dataList.map((item) => {
    //       total += item.number * item.lowDecimal;
    //     });
    //     return total;
    //   },
    //   set: function () {
    //     return 0;
    //   },
    // },
    mainTabsActiveName: {
      get() {
        return this.$store.state.common.mainTabsActiveName;
      },
      set(val) {
        this.$store.commit("common/updateMainTabsActiveName", val);
      },
    },
    mainTabs: {
      get() {
        return this.$store.state.common.mainTabs;
      },
      set(val) {
        this.$store.commit("common/updateMainTabs", val);
      },
    },
  },
};
</script>
<style scoped>
.el-card__body .listHead {
  margin-top: -20px;
  margin-bottom: 5px;
  border-bottom: 1px solid #eeeeee;
  font-size: 14px;
  display: flex;
  justify-content: space-between;
}
.el-form-item {
  width: 24%;
  justify-content: space-between;
  margin-bottom: 15px;
}
.el-form-item >>> .el-input {
  width: 220px;
}
.el-form-item >>> .el-form-item__content {
  display: flex;
}
.el-form-item >>> .el-form-item__content em {
  width: 100px;
  text-align: right;
  color: #31333d;
  margin-right: 15px;
  font-style: normal;
}
.block .el-date-editor .el-range-input {
  width: 50%;
}
.headInfo {
  margin-top: 20px;
}
.main >>> .el-table th,
.el-table tr {
  background-color: #f0f5fa;
  color: #31333d;
}
.customer i {
  position: absolute;
  right: 65px;
  top: 10px;
}
.customer {
  position: relative;
}
.main >>> .has-gutter tr td.el-table_1_column_2 .cell,
.main >>> .has-gutter tr td.el-table_1_column_5 .cell,
.main >>> .has-gutter tr td.el-table_1_column_7 .cell,
.main >>> .has-gutter tr td.el-table_1_column_9 .cell,
.main >>> .has-gutter tr td.el-table_1_column_8 .cell,
.main >>> .has-gutter tr td.el-table_1_column_12 .cell,
.main >>> .has-gutter tr td.el-table_1_column_11 .cell,
.main >>> .has-gutter tr td.el-table_1_column_3 .cell,
.main >>> .has-gutter tr td.el-table_1_column_4 .cell {
  display: none;
}
.footer {
  margin-top: 20px;
}
.form1 >>> .el-form-item {
  width: 100%;
  margin-top: 12px;
}
.form1 >>> .el-form-item .el-input {
  width: auto;
}
.el-form-item.is-required em:before {
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
.listHead >>> .el-button {
  width: 62px;
  height: 30px;
  line-height: 0px;
  background: #f2f9ff;
  border: 1px solid #3153c8;
  color: #3153c8;
  font-size: 14px;
}
.listHead >>> .el-button span {
  margin-left: -4px;
}
.listHead >>> .el-button:first-child {
  width: 62px;
  height: 30px;
  background: #3153c8;
  color: #fff;
}
.mod-user >>> .remarkFooter {
  width: 1617px;
  background: #f5f5f5;
  border: 1px solid #dddddd;
  border-radius: 4px;
  line-height: 40px;
  margin-top: 10px;
  margin-bottom: 10px;
  font-size: 14px;
  color: #666973;
  padding-left: 11px;
}
.main >>> .el-table__footer-wrapper tbody td,
.main >>> .el-table__header-wrapper tbody td {
  background-color: #f0f5fa;
  color: #606266;
}
.main >>> h2 {
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #31333d;
  display: block;
  margin-top: 30px;
  margin-bottom: 15px;
}
</style>
